#include "FilePath.h"
#include "DebugLog.h"
#include <fstream>
#include <physfs.h>
#include <exception>
#include "rapidxml.hpp"

#ifdef _WINDOWS
	#include <Windows.h>
#endif

FilePath::FilePath(void) : _filepath(""){}
FilePath::~FilePath(void){}

void FilePath::parse()
{
	if (_filepath == "")
	{
		Debug::log("ERROR: Filepath not specified");
		#ifdef _WINDOWS
			MessageBox(NULL, "Filepath not specified. Exiting..", "ERROR", MB_ICONSTOP);
		#endif
		abort();
	}

	PHYSFS_file* file = PHYSFS_openRead(_filepath.c_str());
	PHYSFS_sint64 file_size = PHYSFS_fileLength(file);
	char* buffer = new char[static_cast<PHYSFS_sint32>(file_size)];
	PHYSFS_read(file, buffer, 1, static_cast<PHYSFS_uint32>(file_size));
	buffer[file_size] = '\0';

	std::string sb = "";
	for (int i = 0; i < file_size + 1; i++)
	{

		if ( (buffer[i + 1] == '\n' && buffer[i] == '\r') || (buffer[i] == '\0') )
		{
			size_t find = sb.find_first_of("=");
			_filepaths.insert(std::make_pair(sb.substr(0, find), sb.substr(find + 1, sb.length() - find)));
			sb = "";
		}
		else
		{
			if (buffer[i] != '\n' && buffer[i] != '\r' && buffer[i] != ' ')
			{
				sb += buffer[i];
			}
		}
	}
	PHYSFS_close(file);
	PHYSFS_delete(buffer);
}

void FilePath::parseFromFile()
{
	std::ifstream in(_filepath);

	if (!in)
	{
		Debug::log("ERROR: Filepath not specified");
		#ifdef _WINDOWS
			MessageBox(NULL, "Filepath not specified. Exiting..", "ERROR", MB_ICONSTOP);
		#endif
		abort();
	}

	std::string line;
	while (std::getline(in, line))
	{
		size_t find = line.find_first_of("=");
		_filepaths.insert(std::make_pair(line.substr(0, find), line.substr(find + 1, line.length() - find)));
	}
	in.close();
}

const std::string& FilePath::getFilepath(const std::string& id)
{
	m_ss::iterator it = _filepaths.find(id);
	if (it == _filepaths.end())
	{
		Debug::log("ERROR: cannot find " + id + " when reading " + _filepath);
		#ifdef _WINDOWS
			MessageBox(NULL, "Cannot find correct filepath in pak.pkg, check log for more information. Exiting..", "ERROR", MB_ICONSTOP);
		#endif
		abort();
	}
	return _filepaths[id];
}

void FilePath::loadXML()
{
	if (_filepath == "")
	{
		Debug::log("ERROR: Filepath not specified");
		#ifdef _WINDOWS
			MessageBox(NULL, "Filepath not specified. Exiting..", "ERROR", MB_ICONSTOP);
		#endif
		abort();
	}

	PHYSFS_file* file = PHYSFS_openRead(_filepath.c_str());
	PHYSFS_sint64 file_size = PHYSFS_fileLength(file);
	char* buffer = new char[static_cast<PHYSFS_sint32>(file_size)];
	PHYSFS_read(file, buffer, 1, static_cast<PHYSFS_uint32>(file_size));
	buffer[file_size] = '\0';

	std::string xmlContent = buffer;
	std::vector<char> _xmlBuffer = std::vector<char>(xmlContent.begin(), xmlContent.end());
	_xmlBuffer.push_back('\0');
	rapidxml::xml_document<> doc;
	doc.parse<rapidxml::parse_no_data_nodes>(&_xmlBuffer[0]);
	rapidxml::xml_node<>* root = doc.first_node();
	rapidxml::xml_node<>* imageFile = root->first_node("imagefile");
	std::string imagePath = imageFile->first_attribute("path")->value();

	PHYSFS_close(file);
	PHYSFS_delete(buffer);
}